﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using SimpleServiceBus.Bus;
using SimpleServiceBus.Bus.MessageManagement;
using SimpleServiceBus.Bus.Pipeline;
using SimpleServiceBus.Bus.Pipeline.PipelineComponents.MessageFiltering;

namespace SimpleServiceBus.Tests.Bus.Pipeline.PipelineComponents.MessageFiltering
{
    [TestFixture]
    public class MessageFilterServiceTestFixture
    {
        [Test]
        public void MatchesShouldReturnTrueWhenNoFiltersAreSpecifiedForAPipelineDirection()
        {
            //Given
            var message = new MessageEnvelope();

            var filter = new MessageFilterService();
            var predicate = MockRepository.GenerateStub<IMessagePredicate>();
            predicate.Stub(p => p.Matches(message)).Return(false);

            filter.RegisterMessageFilter(typeof(object),predicate, PipelineDirection.Send);

            //When
            var result = filter.Matches(message, PipelineDirection.Receive);

            //Then
            Assert.AreEqual(true, result);
        }

        [Test]
        public void MatchesShouldQueryRegisteredPredicatesToDetermineTrueResult()
        {
            //Given
            var message = new MessageEnvelope();

            var filter = new MessageFilterService();
            var predicate = MockRepository.GenerateStub<IMessagePredicate>();
            predicate.Stub(p => p.Matches(message)).Return(true);

            filter.RegisterMessageFilter(typeof(object),predicate, PipelineDirection.Send);

            //When
            var result = filter.Matches(message, PipelineDirection.Send);

            //Then
            Assert.AreEqual(true, result);
        }

        [Test]
        public void MatchesShouldQueryRegisteredPredicatesToDetermineFalseResult()
        {
            //Given
            var message = new MessageEnvelope();

            var filter = new MessageFilterService();
            var predicate = MockRepository.GenerateStub<IMessagePredicate>();
            predicate.Stub(p => p.Matches(message)).Return(false);

            filter.RegisterMessageFilter(typeof(object),predicate, PipelineDirection.Send);

            //When
            var result = filter.Matches(message, PipelineDirection.Send);

            //Then
            Assert.AreEqual(false, result);
        }
    }
}
